<?php

namespace app\storeapi\controller\apps\sharp;

use app\storeapi\controller\Controller;
use app\storeapi\model\sharp\Active as ActiveModel;

/**
 * 秒杀活动会场管理
 * Class Active
 * @package app\store\controller\apps\sharp
 */
class Active extends Controller
{
    /**
     * 活动会场列表
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $param = $this->postData();
        $model = new ActiveModel;
        $list = $model->getList($param);
        return $this->renderSuccess($list);
    }

    /**
     * 删除活动会场
     * @param $active_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        $param = $this->postData();
        // 活动会场详情
        $model = ActiveModel::detail($param['active_id']);
        if(!$model){
            return $this->renderError("该会场不存在，请刷新后重试");
        }
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess([],'删除成功');
    }

    /**
     * 修改活动状态
     * @param $active_id
     * @param $state
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function status()
    {
        $param = $this->postData();
        // 活动详情
        $model = ActiveModel::detail($param['active_id']);
        if(!$model){
            return $this->renderError("该会场不存在，请刷新后重试");
        }
        if (!$model->setStatus($param['status'])) {
            return $this->renderError('操作失败');
        }
        return $this->renderSuccess([],'操作成功');
    }



    /**
     * 新增活动会场
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $param = $this->postData();
        $model = new ActiveModel;
        // 新增记录
        if ($model->add($param)) {
            return $this->renderSuccess([],'添加成功');
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }


    /**
     * 新增活动会场
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function getTimes()
    {
        $param = $this->postData();
        $model = new ActiveModel;
        $list = $model->getTimes($param);
        return $this->renderSuccess($list);
    }



}